package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.DBDataClass.SysStudyDataClass;
import com.limao.DBProvider.SysStudyProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class Sys_StudyCacheManager extends BasicModule{

	private static Sys_StudyCacheManager instance;
	private SysStudyProvider provider;
	
    public static Sys_StudyCacheManager getInstance() {
        return instance;
    }

    private Cache<String, SysStudyDataClass> SySNewStudy_ManagerCache;
    private Cache<String, SysStudyDataClass> SySRule_ManagerCache;

    public Sys_StudyCacheManager() {
    	super("Sys_StudyCacheManager");
    	SySNewStudy_ManagerCache = CacheFactory.createCache("cd_sysNewStudyCache");
    	SySRule_ManagerCache = CacheFactory.createCache("cd_sysRuleCache");
    	initProvider();
    }
    
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    

    public void ClearSysNewStudyCache()
    {
    	SySNewStudy_ManagerCache.clear();
    }  
    public void ClearSysRuleCache()
    {
    	SySRule_ManagerCache.clear();
    }   
    
    //**************************************************************************
 
    //新鲜宝  系统新手学堂资料
    public ArrayList<SysStudyDataClass> Load_SySMatter_All()
    {
    	ArrayList<SysStudyDataClass> list=new ArrayList<SysStudyDataClass>();
        Set set =SySNewStudy_ManagerCache.keySet();
    	Iterator it=set.iterator();
    	while(it.hasNext()){
     		String s= (String) it.next();
    		list.add(SySNewStudy_ManagerCache.get(s));
    	}
    	
    	if(list.size()==0)
    	{
    		list=provider.Load_SySNewStudy_All();
    		if(list!=null)
    		{
    			for(SysStudyDataClass obj:list)
    			{
    				SySNewStudy_ManagerCache.put(obj.studyid,obj);
    			}
    		}
    	}


    	Collections.sort(list, new Comparator<SysStudyDataClass>() {
    		
    		public int compare(SysStudyDataClass arg0, SysStudyDataClass arg1) {
    				return ((Integer)CacheClass.getIntValue(arg1.studysort)).compareTo(CacheClass.getIntValue(arg0.studysort));
    		}
    	});
    	
    	return list;
    }  
    
    //全部  公司活动
    public ArrayList<SysStudyDataClass> Load_SySNewRule_All()
    {
    	ArrayList<SysStudyDataClass> list=new ArrayList<SysStudyDataClass>();
        Set set =SySRule_ManagerCache.keySet();
    	Iterator it=set.iterator();
    	while(it.hasNext()){
     		String s= (String) it.next();
    		list.add(SySRule_ManagerCache.get(s));
    	}
    	
    	if(list.size()==0)
    	{
    		list=provider.Load_SySNewRule_All();
    		if(list!=null)
    		{
    			for(SysStudyDataClass obj:list)
    			{
    				SySRule_ManagerCache.put(obj.studyid,obj);
    			}
    		}
    	}

    	
    	Collections.sort(list, new Comparator<SysStudyDataClass>() {
    		
    		public int compare(SysStudyDataClass arg0, SysStudyDataClass arg1) {
    				return ((Integer)CacheClass.getIntValue(arg1.studysort)).compareTo(CacheClass.getIntValue(arg0.studysort));
    		}
    	});
    	
    	return list;
    }     
    
    
    public SysStudyDataClass Load_SySNewRule_ById(String studyid)
    {
/*    	SysStudyDataClass tmp=SySRule_ManagerCache.get(studyid);
    	if(tmp==null)
    	{
    		tmp=provider.Load_SySNewRule_ById(CacheClass.getIntValue(studyid));
    	}*/
    	return provider.Load_SySNewRule_ById(CacheClass.getIntValue(studyid));
    }
    
    //**************************************************************************
    private void initProvider() {
        if (provider == null ) {
            try {
                provider =SysStudyProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
    }
}
